package com.example.bbs.ui.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.bbs.R;
import com.example.bbs.base.Data;
import com.example.bbs.config.HttpConstant;
import com.example.bbs.util.BaseUtil;
import com.example.bbs.util.HttpUtil;

import java.util.concurrent.CompletableFuture;

import okhttp3.FormBody;

public class PersonalInfo  extends AppCompatActivity {
    private AlertDialog dlg;
    private TextView tv_user;
    private TextView tv_email;
    private AlertDialog dlg_per;
    private TextView tv_per;
    private ImageView iv_avatar;

    private int post_author_id = -1;
    static final String LOG_TAG = PersonalInfo.class.getSimpleName();

    ImageView btn_block;//屏蔽
    TextView tv_block;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_info);
        tv_user = findViewById(R.id.tv_user);
        tv_email = findViewById(R.id.tv_email);
        tv_per = findViewById(R.id.tv_per);
        tv_block = findViewById(R.id.tv_block);
        btn_block = findViewById(R.id.btn_block);
        iv_avatar = findViewById(R.id.iv_avatar);
        Listener listener = new Listener();
        btn_block.setOnClickListener(listener);
    }

    class Listener implements View.OnClickListener {

        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_block:
                    BaseUtil.showToast("block");
                    block();
                    break;
            }
        }
    }

    private void load() {
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        post_author_id = bundle.getInt("id",-1);
        Log.d(LOG_TAG,"post_author_id:" + post_author_id);

        if(post_author_id == Data.myself.getId()){
            btn_block.setVisibility(View.INVISIBLE);
            tv_block.setVisibility(View.INVISIBLE);
        }
        get_personal_info(post_author_id);

        //TODO:get_user暂时无法获取邮箱

    }

    private void get_personal_info(int id){
        FormBody formBody = new FormBody.Builder()
                .add(HttpConstant.FIELD_USERID, "" + id)
                .build();
        JSONObject json = HttpUtil.post(HttpConstant.REQUEST_GET_USER, formBody);
        if (json == null || !json.getBoolean(HttpConstant.FIELD_REPLY))
            return;
        JSONObject result = json.getJSONObject(HttpConstant.FIELD_DATA);
        Log.d(LOG_TAG, "" + result);
        String name = result.getString("name");
        String introduction = result.getString("introduction");
        String email = result.getString("email");
        tv_user.setText(name);
        tv_per.setText(introduction);
        tv_email.setText(email);
    }

    private void block(){
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        post_author_id = bundle.getInt("id",-1);
        Log.d(LOG_TAG,"" + post_author_id);
        FormBody formBody = new FormBody.Builder()
                .add(HttpConstant.FIELD_USERID, "" + post_author_id)
                .build();
        HttpUtil.postAsync(HttpConstant.REQUEST_BLOCK, formBody, (response) -> {
            if (response != null  && response.getBoolean(HttpConstant.FIELD_REPLY))
                Log.i(LOG_TAG, "block success");
            else
                Log.w(LOG_TAG, "block failure");
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(LOG_TAG, "onResume");

        try {
            CompletableFuture.runAsync(this::load).get(); // 初始化
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(LOG_TAG, "onPause");
    }
}
